package it.unibz.easytravel.serverconnection;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.DefaultHttpClient;

public class ServerConnection {

	
	public static void sendZip(File file) throws ClientProtocolException, IOException {
		HttpClient httpclient = new DefaultHttpClient();
		
		URI uri = URI.create("blah");
		
		
        HttpPost httppost = new HttpPost();
                
        httppost.setHeader("msg_type", MessageType.KML_REQUEST.name());
        
        FileEntity reqEntity = new FileEntity(file, "binary/octet-stream");
        
        reqEntity.setContentType("binary/octet-stream");
        
        httppost.setEntity(reqEntity);
        
        System.out.println("executing request " + httppost.getRequestLine());
        HttpResponse response = httpclient.execute(httppost);
        
        HttpEntity resEntity = response.getEntity();

        if (resEntity != null) {
            System.out.println("Response content length: " + resEntity.getContentLength());
            System.out.println("Chunked?: " + resEntity.isChunked());
        }
        
        InputStream i = resEntity.getContent();
        int b;
        while ((b = i.read()) != -1) {
        	System.out.print((char)b);
        }
        
        if (resEntity != null) {
			resEntity.consumeContent();
        }
       
		return;
	}
	
}
